This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
market_basket <- list(
c("apple","beer","rice","meat"),
c("apple","beer","rice"),
c("apple","pear"),
c("apple","beer"),
c("milk","beer","rice","meat"),
c("milk","beer","rice"),
c("milk","beer"),
c("milk","pear")
)
names(market_basket) <- paste("T -",c(1:8),sep = " ")
print(market_basket)
$`T - 1`
[1] "apple" "beer" "rice" "meat"
$`T - 2`
[1] "apple" "beer" "rice"
$`T - 3`
[1] "apple" "pear"
$`T - 4`
[1] "apple" "beer"
$`T - 5`
[1] "milk" "beer" "rice" "meat"
$`T - 6`
[1] "milk" "beer" "rice"
$`T - 7`
[1] "milk" "beer"
$`T - 8`
[1] "milk" "pear"
install.packages("arules")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/prayas gautam/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
also installing the dependency ‘generics’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/generics_0.1.3.zip'
Content type 'application/zip' length 80337 bytes (78 KB)
downloaded 78 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/arules_1.7-4.zip'
Content type 'application/zip' length 2125385 bytes (2.0 MB)
downloaded 2.0 MB
package ‘generics’ successfully unpacked and MD5 sums checked
package ‘arules’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\prayas gautam\AppData\Local\Temp\RtmpofkB9d\downloaded_packages
library(arules)
Warning: package ‘arules’ was built under R version 4.2.1
Loading required package: Matrix
Attaching package: ‘arules’
The following objects are masked from ‘package:base’:
abbreviate, write
install.packages("arulesViz")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Installing package into ‘C:/Users/prayas gautam/AppData/Local/R/win-library/4.2’
(as ‘lib’ is unspecified)
also installing the dependencies ‘sys’, ‘iterators’, ‘askpass’, ‘foreach’, ‘zoo’, ‘tweenr’, ‘polyclip’, ‘RcppEigen’, ‘RcppArmadillo’, ‘later’, ‘curl’, ‘openssl’, ‘TSP’, ‘qap’, ‘gclus’, ‘registry’, ‘lmtest’, ‘ggforce’, ‘ggrepel’, ‘viridis’, ‘tidygraph’, ‘graphlayouts’, ‘crosstalk’, ‘promises’, ‘httr’, ‘lazyeval’, ‘seriation’, ‘vcd’, ‘ggraph’, ‘tidyr’, ‘dplyr’, ‘DT’, ‘plotly’, ‘visNetwork’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/sys_3.4.zip'
Content type 'application/zip' length 46650 bytes (45 KB)
downloaded 45 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/iterators_1.0.14.zip'
Content type 'application/zip' length 353367 bytes (345 KB)
downloaded 345 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/askpass_1.1.zip'
Content type 'application/zip' length 72249 bytes (70 KB)
downloaded 70 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/foreach_1.5.2.zip'
Content type 'application/zip' length 148029 bytes (144 KB)
downloaded 144 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/zoo_1.8-11.zip'
Content type 'application/zip' length 1019273 bytes (995 KB)
downloaded 995 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/tweenr_2.0.2.zip'
Content type 'application/zip' length 526894 bytes (514 KB)
downloaded 514 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/polyclip_1.10-0.zip'
Content type 'application/zip' length 390354 bytes (381 KB)
downloaded 381 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/RcppEigen_0.3.3.9.2.zip'
Content type 'application/zip' length 2283890 bytes (2.2 MB)
downloaded 2.2 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/RcppArmadillo_0.11.2.4.0.zip'
Content type 'application/zip' length 1970587 bytes (1.9 MB)
downloaded 1.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/later_1.3.0.zip'
Content type 'application/zip' length 455001 bytes (444 KB)
downloaded 444 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/curl_4.3.2.zip'
Content type 'application/zip' length 2616955 bytes (2.5 MB)
downloaded 2.5 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/openssl_2.0.3.zip'
Content type 'application/zip' length 2651001 bytes (2.5 MB)
downloaded 2.5 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/TSP_1.2-1.zip'
Content type 'application/zip' length 947260 bytes (925 KB)
downloaded 925 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/qap_0.1-2.zip'
Content type 'application/zip' length 529590 bytes (517 KB)
downloaded 517 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/gclus_1.3.2.zip'
Content type 'application/zip' length 416203 bytes (406 KB)
downloaded 406 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/registry_0.5-1.zip'
Content type 'application/zip' length 197294 bytes (192 KB)
downloaded 192 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/lmtest_0.9-40.zip'
Content type 'application/zip' length 405804 bytes (396 KB)
downloaded 396 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/ggforce_0.3.4.zip'
Content type 'application/zip' length 2184172 bytes (2.1 MB)
downloaded 2.1 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/ggrepel_0.9.1.zip'
Content type 'application/zip' length 729625 bytes (712 KB)
downloaded 712 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/viridis_0.6.2.zip'
Content type 'application/zip' length 2999889 bytes (2.9 MB)
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/tidygraph_1.2.2.zip'
Content type 'application/zip' length 579894 bytes (566 KB)
downloaded 566 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/graphlayouts_0.8.1.zip'
Content type 'application/zip' length 2961422 bytes (2.8 MB)
downloaded 2.8 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/crosstalk_1.2.0.zip'
Content type 'application/zip' length 410183 bytes (400 KB)
downloaded 400 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/promises_1.2.0.1.zip'
Content type 'application/zip' length 1965920 bytes (1.9 MB)
downloaded 1.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/httr_1.4.4.zip'
Content type 'application/zip' length 518639 bytes (506 KB)
downloaded 506 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/lazyeval_0.2.2.zip'
Content type 'application/zip' length 161184 bytes (157 KB)
downloaded 157 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/seriation_1.3.6.zip'
Content type 'application/zip' length 1152248 bytes (1.1 MB)
downloaded 1.1 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/vcd_1.4-10.zip'
Content type 'application/zip' length 1287535 bytes (1.2 MB)
downloaded 1.2 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/ggraph_2.0.6.zip'
Content type 'application/zip' length 4305404 bytes (4.1 MB)
downloaded 4.1 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/tidyr_1.2.1.zip'
Content type 'application/zip' length 967454 bytes (944 KB)
downloaded 944 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/dplyr_1.0.10.zip'
Content type 'application/zip' length 1300718 bytes (1.2 MB)
downloaded 1.2 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/DT_0.25.zip'
Content type 'application/zip' length 1808725 bytes (1.7 MB)
downloaded 1.7 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/plotly_4.10.0.zip'
Content type 'application/zip' length 3174283 bytes (3.0 MB)
downloaded 3.0 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/visNetwork_2.1.0.zip'
Content type 'application/zip' length 4923855 bytes (4.7 MB)
downloaded 4.7 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.2/arulesViz_1.5-1.zip'
Content type 'application/zip' length 1781350 bytes (1.7 MB)
downloaded 1.7 MB
package ‘sys’ successfully unpacked and MD5 sums checked
package ‘iterators’ successfully unpacked and MD5 sums checked
package ‘askpass’ successfully unpacked and MD5 sums checked
package ‘foreach’ successfully unpacked and MD5 sums checked
package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘tweenr’ successfully unpacked and MD5 sums checked
package ‘polyclip’ successfully unpacked and MD5 sums checked
package ‘RcppEigen’ successfully unpacked and MD5 sums checked
package ‘RcppArmadillo’ successfully unpacked and MD5 sums checked
package ‘later’ successfully unpacked and MD5 sums checked
package ‘curl’ successfully unpacked and MD5 sums checked
package ‘openssl’ successfully unpacked and MD5 sums checked
package ‘TSP’ successfully unpacked and MD5 sums checked
package ‘qap’ successfully unpacked and MD5 sums checked
package ‘gclus’ successfully unpacked and MD5 sums checked
package ‘registry’ successfully unpacked and MD5 sums checked
package ‘lmtest’ successfully unpacked and MD5 sums checked
package ‘ggforce’ successfully unpacked and MD5 sums checked
package ‘ggrepel’ successfully unpacked and MD5 sums checked
package ‘viridis’ successfully unpacked and MD5 sums checked
package ‘tidygraph’ successfully unpacked and MD5 sums checked
package ‘graphlayouts’ successfully unpacked and MD5 sums checked
package ‘crosstalk’ successfully unpacked and MD5 sums checked
package ‘promises’ successfully unpacked and MD5 sums checked
package ‘httr’ successfully unpacked and MD5 sums checked
package ‘lazyeval’ successfully unpacked and MD5 sums checked
package ‘seriation’ successfully unpacked and MD5 sums checked
package ‘vcd’ successfully unpacked and MD5 sums checked
package ‘ggraph’ successfully unpacked and MD5 sums checked
package ‘tidyr’ successfully unpacked and MD5 sums checked
package ‘dplyr’ successfully unpacked and MD5 sums checked
package ‘DT’ successfully unpacked and MD5 sums checked
package ‘plotly’ successfully unpacked and MD5 sums checked
package ‘visNetwork’ successfully unpacked and MD5 sums checked
package ‘arulesViz’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\prayas gautam\AppData\Local\Temp\RtmpofkB9d\downloaded_packages
library(arulesViz)
Warning: package ‘arulesViz’ was built under R version 4.2.1
dim(market_basket)
NULL
trans <- as(market_basket,"transactions")
dim(trans)
[1] 8 6
itemLabels(trans)
[1] "apple" "beer" "meat" "milk" "pear" "rice"
summary(trans)
transactions as itemMatrix in sparse format with
8 rows (elements/itemsets/transactions) and
6 columns (items) and a density of 0.4583333
most frequent items:
beer apple milk rice meat (Other)
6 4 4 4 2 2
element (itemset/transaction) length distribution:
sizes
2 3 4
4 2 2
Min. 1st Qu. Median Mean 3rd Qu. Max.
2.00 2.00 2.50 2.75 3.25 4.00
includes extended item information - examples:
includes extended transaction information - examples:
rules <- apriori(trans,parameter = list(supp = 0.3,conf = 0.5,maxlen = 10,target = "rules"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [10 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rules)
set of 10 rules
rule length distribution (lhs + rhs):sizes
1 2
4 6
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.0 1.0 2.0 1.6 2.0 2.0
summary of quality measures:
support confidence coverage lift
Min. :0.375 Min. :0.5000 Min. :0.5000 Min. :1.000
1st Qu.:0.375 1st Qu.:0.5000 1st Qu.:0.5625 1st Qu.:1.000
Median :0.500 Median :0.5833 Median :0.7500 Median :1.000
Mean :0.475 Mean :0.6417 Mean :0.7750 Mean :1.067
3rd Qu.:0.500 3rd Qu.:0.7500 3rd Qu.:1.0000 3rd Qu.:1.000
Max. :0.750 Max. :1.0000 Max. :1.0000 Max. :1.333
count
Min. :3.0
1st Qu.:3.0
Median :4.0
Mean :3.8
3rd Qu.:4.0
Max. :6.0
mining info:
beer_ass_rule <- apriori(trans,
parameter = list(supp = 0.3,conf = 0.5,maxlen = 10,minlen = 2),appearance = list(default = "rhs",lhs = "beer"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 2
set item appearances ...[1 item(s)] done [0.00s].
set transactions ...[6 item(s), 8 transaction(s)] done [0.00s].
sorting and recoding items ... [4 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 done [0.00s].
writing ... [3 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
inspect(beer_ass_rule)
plot(rules,engine = "plotly")
To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.
Registered S3 method overwritten by 'data.table':
method from
print.data.table
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
subrules <- head(rules,n =10,by = "confidence")
data(Groceries)
class(Groceries)
[1] "transactions"
attr(,"package")
[1] "arules"
dim(Groceries)
[1] 9835 169
print(Groceries)
transactions in sparse format with
9835 transactions (rows) and
169 items (columns)
itemLabels(Groceries)
[1] "frankfurter" "sausage" "liver loaf"
[4] "ham" "meat" "finished products"
[7] "organic sausage" "chicken" "turkey"
[10] "pork" "beef" "hamburger meat"
[13] "fish" "citrus fruit" "tropical fruit"
[16] "pip fruit" "grapes" "berries"
[19] "nuts/prunes" "root vegetables" "onions"
[22] "herbs" "other vegetables" "packaged fruit/vegetables"
[25] "whole milk" "butter" "curd"
[28] "dessert" "butter milk" "yogurt"
[31] "whipped/sour cream" "beverages" "UHT-milk"
[34] "condensed milk" "cream" "soft cheese"
[37] "sliced cheese" "hard cheese" "cream cheese "
[40] "processed cheese" "spread cheese" "curd cheese"
[43] "specialty cheese" "mayonnaise" "salad dressing"
[46] "tidbits" "frozen vegetables" "frozen fruits"
[49] "frozen meals" "frozen fish" "frozen chicken"
[52] "ice cream" "frozen dessert" "frozen potato products"
[55] "domestic eggs" "rolls/buns" "white bread"
[58] "brown bread" "pastry" "roll products "
[61] "semi-finished bread" "zwieback" "potato products"
[64] "flour" "salt" "rice"
[67] "pasta" "vinegar" "oil"
[70] "margarine" "specialty fat" "sugar"
[73] "artif. sweetener" "honey" "mustard"
[76] "ketchup" "spices" "soups"
[79] "ready soups" "Instant food products" "sauces"
[82] "cereals" "organic products" "baking powder"
[85] "preservation products" "pudding powder" "canned vegetables"
[88] "canned fruit" "pickled vegetables" "specialty vegetables"
[91] "jam" "sweet spreads" "meat spreads"
[94] "canned fish" "dog food" "cat food"
[97] "pet care" "baby food" "coffee"
[100] "instant coffee" "tea" "cocoa drinks"
[103] "bottled water" "soda" "misc. beverages"
[106] "fruit/vegetable juice" "syrup" "bottled beer"
[109] "canned beer" "brandy" "whisky"
[112] "liquor" "rum" "liqueur"
[115] "liquor (appetizer)" "white wine" "red/blush wine"
[118] "prosecco" "sparkling wine" "salty snack"
[121] "popcorn" "nut snack" "snack products"
[124] "long life bakery product" "waffles" "cake bar"
[127] "chewing gum" "chocolate" "cooking chocolate"
[130] "specialty chocolate" "specialty bar" "chocolate marshmallow"
[133] "candy" "seasonal products" "detergent"
[136] "softener" "decalcifier" "dish cleaner"
[139] "abrasive cleaner" "cleaner" "toilet cleaner"
[142] "bathroom cleaner" "hair spray" "dental care"
[145] "male cosmetics" "make up remover" "skin care"
[148] "female sanitary products" "baby cosmetics" "soap"
[151] "rubbing alcohol" "hygiene articles" "napkins"
[154] "dishes" "cookware" "kitchen utensil"
[157] "cling film/bags" "kitchen towels" "house keeping products"
[160] "candles" "light bulbs" "sound storage medium"
[163] "newspapers" "photo/film" "pot plants"
[166] "flower soil/fertilizer" "flower (seeds)" "shopping bags"
[169] "bags"
rule <- apriori(Groceries,parameter = list(supp = 0.01,conf = 0.5,minlen = 2,target = "rule"))
Apriori
Parameter specification:
Algorithmic control:
Absolute minimum support count: 98
set item appearances ...[0 item(s)] done [0.00s].
set transactions ...[169 item(s), 9835 transaction(s)] done [0.01s].
sorting and recoding items ... [88 item(s)] done [0.00s].
creating transaction tree ... done [0.00s].
checking subsets of size 1 2 3 4 done [0.01s].
writing ... [15 rule(s)] done [0.00s].
creating S4 object ... done [0.00s].
summary(rule)
set of 15 rules
rule length distribution (lhs + rhs):sizes
3
15
Min. 1st Qu. Median Mean 3rd Qu. Max.
3 3 3 3 3 3
summary of quality measures:
support confidence coverage lift count
Min. :0.01007 Min. :0.5000 Min. :0.01729 Min. :1.984 Min. : 99.0
1st Qu.:0.01174 1st Qu.:0.5151 1st Qu.:0.02089 1st Qu.:2.036 1st Qu.:115.5
Median :0.01230 Median :0.5245 Median :0.02430 Median :2.203 Median :121.0
Mean :0.01316 Mean :0.5411 Mean :0.02454 Mean :2.299 Mean :129.4
3rd Qu.:0.01403 3rd Qu.:0.5718 3rd Qu.:0.02598 3rd Qu.:2.432 3rd Qu.:138.0
Max. :0.02227 Max. :0.5862 Max. :0.04342 Max. :3.030 Max. :219.0
mining info:
inspect(rule)
data(AdultUCI)
class(AdultUCI)
[1] "data.frame"
AdultUCI <- lapply(AdultUCI, function(x) {as.factor(x)})
str(AdultUCI)
List of 15
$ age : Factor w/ 74 levels "17","18","19",..: 23 34 22 37 12 21 33 36 15 26 ...
$ workclass : Factor w/ 8 levels "Federal-gov",..: 7 6 4 4 4 4 4 6 4 4 ...
$ fnlwgt : Factor w/ 28523 levels "12285","13492",..: 3462 3789 18343 19996 25406 23150 11188 17736 1680 11094 ...
$ education : Ord.factor w/ 16 levels "Preschool"<"1st-4th"<..: 14 14 9 7 14 15 5 9 15 14 ...
$ education-num : Factor w/ 16 levels "1","2","3","4",..: 13 13 9 7 13 14 5 9 14 13 ...
$ marital-status: Factor w/ 7 levels "Divorced","Married-AF-spouse",..: 5 3 1 3 3 3 4 3 5 3 ...
$ occupation : Factor w/ 14 levels "Adm-clerical",..: 1 4 6 6 10 4 8 4 10 4 ...
$ relationship : Factor w/ 6 levels "Husband","Not-in-family",..: 2 1 2 1 6 6 2 1 2 1 ...
$ race : Factor w/ 5 levels "Amer-Indian-Eskimo",..: 5 5 5 3 3 5 3 5 5 5 ...
$ sex : Factor w/ 2 levels "Female","Male": 2 2 2 2 1 1 1 2 1 2 ...
$ capital-gain : Factor w/ 123 levels "0","114","401",..: 28 1 1 1 1 1 1 1 110 82 ...
$ capital-loss : Factor w/ 99 levels "0","155","213",..: 1 1 1 1 1 1 1 1 1 1 ...
$ hours-per-week: Factor w/ 96 levels "1","2","3","4",..: 40 13 40 40 40 40 16 45 50 40 ...
$ native-country: Factor w/ 41 levels "Cambodia","Canada",..: 39 39 39 39 5 39 23 39 39 39 ...
$ income : Ord.factor w/ 2 levels "small"<"large": 1 1 1 1 1 1 1 2 2 2 ...
transaction <- as(AdultUCI,"transactions")
Warning in asMethod(object) : removing duplicated items in transactions
Error in validObject(.Object) :
invalid class “ngCMatrix” object: row indices are not sorted within columns
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.